<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
/**
 * PHP version 5
 *
 * This source file is subject to version 2.1 of the GNU Lesser General Public
 * License, that is bundled with this package in the file COPYING, available
 * through the world wide web at the following URI:
 * http://www.gnu.org/copyleft/lesser.html.
 *
 * @package    ZNF
 * @subpackage Presentation
 * @author     Alessandro Rossini <http://www.alessandrorossini.org>
 * @author     Graziano Liberati <http://www.liberati.org>
 * @copyright  2004-2007 The ZNF Development Team
 * @license    LGPL License 2.1 <http://www.gnu.org/copyleft/lesser.html>
 * @version    SVN $Id: Render.php 44 2007-07-22 23:55:30Z evolic $
 * @since      Release 0.6.2
 * @link       http://www.zeronotice.org
 */

require_once('ZNF/Config/AppConfig.php');

/**
 * <i>ZNF_Presentation_Render</i> specifies the abstract class that renderization classes should extends.
 *
 * @access     public
 * @package    ZNF
 * @subpackage Presentation
 * @author     Alessandro Rossini <http://www.alessandrorossini.org>
 * @author     Graziano Liberati <http://www.liberati.org>
 * @copyright  2004-2007 The ZNF Development Team
 * @license    LGPL License 2.1 <http://www.gnu.org/copyleft/lesser.html>
 * @version    SVN $Id: Render.php 44 2007-07-22 23:55:30Z evolic $
 * @since      Release 0.6.2
 * @link       http://www.zeronotice.org
 */
abstract class ZNF_Presentation_Render
{

    /**
     * The <i>ZNF_Config_AppConfig</i> instance.
     *
     * @access protected
     * @var ZNF_Config_AppConfig $_appConfig
     */
    protected $_appConfig;

    /**
     * The name of the package to which the template refers.
     *
     * @access protected
     * @var string $_package
     */
    protected $_package = null;

    /**
     * The <i>path</i> property of the <i>ZNF_Action_ActionMapping</i>.
     *
     * @access protected
     * @var string $_path
     */
    protected $_path = null;

    /**
     * The <i>nextPath</i> property of the <i>ZNF_Action_ActionMapping</i>.
     *
     * @access protected
     * @var string $_nextPath
     */
    protected $_nextPath = null;

    /**
     * Constructs a new <i>ZNF_Presentation_Render</i> object and sets the internal properties with application settings.
     *
     * @access public
     * @param string $package Name of the package to which the template refers,
     *                        leave null this parameter to refer to the theme
     *                        template
     * @param ZNF_Action_ActionMapping $mapping
     */
    public function __construct($package = null, $mapping = null)
    {
        $this->_appConfig = ZNF_Config_AppConfig::getInstance();
        $this->_package = $package;

        if ($mapping) {
            $this->_path = $mapping->getPath();
            $this->_nextPath = $mapping->getNextPath();
        }
    }

    /**
     * Get the ZNF constants to be assigned to the templates.
     *
     * <ul>
     *   <li>znf.baseHref: concatenation of hostname and relative path of the
     *   public script directory, used as prefix in hyperlinks in order to
     *   refer to correct URIs.</li>
     *   <li>znf.actionIndex: Name of the GET attribute used to select the
     *   action to execute.</li>
     *   <li>znf.moduleIndex: Name of the GET attribute used to select the
     *   module configuration.</li>
     *   <li>znf.changeModuleIndex: Name of the GET attribute used to select
     *   persistently the module configuration.</li>
     *   <li>znf.themePath: relative path of the current theme, used as prefix
     *   in hyperlinks (css, javascripts, images...) in order to refer to
     *   correct paths.</li>
     *   <li>znf.packagePath: relative path of the package in use, used as
     *   prefix in hyperlinks (css, javascripts, images...) in order to refer
     *   to correct paths.</li>
     *   <li>znf.path: current action path.</li>
     *   <li>znf.nextPath: next action path to request.</li>
     *   <li>znf.credits: ZNF credits.</li>
     * </ul>
     *
     * @access protected
     * @return array
     */
    protected function _getConstants()
    {
        if (isset($_SERVER['HTTPS'])) {
          $protocol = 'https://';
        } else {
          $protocol = 'http://';
        }

        $constants['baseHref'] = $protocol . "{$_SERVER['SERVER_NAME']}";
        if ($_SERVER['SERVER_PORT'] != '80' && $_SERVER['SERVER_PORT'] != '443') {
            $constants['baseHref'] .= ":{$_SERVER['SERVER_PORT']}";
        }
        if (dirname($_SERVER['SCRIPT_NAME']) != '/') {
            $constants['baseHref'] .=  dirname($_SERVER['SCRIPT_NAME']);
        }
        $constants['baseHref']          .= '/';
        $constants['actionIndex']       = ZNF::ACTION_INDEX;
        $constants['moduleIndex']       = ZNF::MODULE_INDEX;
        $constants['changeModuleIndex'] = ZNF::CHANGE_MODULE_INDEX;
        $constants['themePath']         = $this->_getThemePath();
        $constants['packagePath']       = $this->_package;
        $constants['path']              = $this->_path;
        $constants['nextPath']          = $this->_nextPath;
        $constants['credits']           = ZNF::CREDITS;

        return $constants;
    }

    /**
     * Returns the path of a theme template, tries to load the template in a predefined order.
     *
     * <ul>
     *   <li>current theme and current lang template</li>
     *   <li>current theme and default lang template</li>
     *   <li>default theme and default lang template</li>
     * </ul>
     *
     * @access protected
     * @param string $template File name of the template to get
     * @return string
     */
    protected function _getThemeTemplatePath($template)
    {
        // loading current theme and current lang template
        $path = ZNF::THEME_DIR . "/{$_SESSION['znf']['theme']}/{$_SESSION['znf']['lang']}/{$template}";

        if (!is_file($path)) {
            // loading current theme and default lang template
            $path = ZNF::THEME_DIR . "/{$_SESSION['znf']['theme']}/{$this->_appConfig->getLang()}/{$template}";

            if (!is_file($path)) {
                // loading default theme and default lang template
                $path = ZNF::THEME_DIR . "/{$this->_appConfig->getTheme()}/{$this->_appConfig->getLang()}/{$template}";

                if (!is_file($path)) {
                    // default theme and default lang template not found
                    $translation = new ZNF_Presentation_Translation('ZNF', $_SESSION['znf']['lang']);
                    throw new ZNF_Presentation_RenderException($translation->get('errorLoadThemeTemplate'));
                }

            }

        }

        return $path;
    }

    /**
     * Returns the path of a package template, tries to load the template in a predefined order.
     *
     * <ul>
     *   <li>overloaded package template in the current theme and lang path</li>
     *   <li>current package and current lang template</li>
     *   <li>current package and default lang template</li>
     * </ul>
     *
     * @access protected
     * @param string $template File name of the template to get
     * @return string
     */
    protected function _getPackageTemplatePath($template)
    {
        // loading overloaded package template in the current theme and lang path
        $path = ZNF::THEME_DIR . "/{$_SESSION['znf']['theme']}/" . ZNF::TEMPLATE_OVERLAY_DIR . "/{$this->_package}/{$_SESSION['znf']['lang']}/{$template}";

        if (!is_file($path)) {
            // loading current package and current lang template
            $path = ZNF::MODULES_DIR . "/{$this->_package}/" . ZNF::TEMPLATE_DIR . "/{$_SESSION['znf']['lang']}/{$template}";

            if (!is_file($path)) {
                // loading current package and default lang template
                $path = ZNF::MODULES_DIR . "/{$this->_package}/" . ZNF::TEMPLATE_DIR . "/{$this->_appConfig->getLang()}/{$template}";

                if (!is_file($path)) {
                    // current package and default lang template not found
                    $translation = new ZNF_Presentation_Translation('ZNF', $_SESSION['znf']['lang']);
                    throw new ZNF_Presentation_RenderException($translation->get('errorLoadPackageTemplate'));
                }

            }

        }

        return $path;
    }

    /**
     * Returns the path to get the template requested.
     *
     * @access public
     * @param string $template File name of the template
     * @return string
     */
    protected function _getTemplatePath($template)
    {
        if ($this->_package) {
            return $this->_getPackageTemplatePath($template);
        } else {
            return $this->_getThemeTemplatePath($template);
        }
    }

    /**
     * Returns the path of a theme, tries to find the theme base directory in a predefined order.
     *
     * <ul>
     *   <li>current theme and current lang theme base directory</li>
     *   <li>current theme and default lang theme base directory</li>
     *   <li>default theme and default lang theme base directory</li>
     * </ul>
     *
     * @access protected
     * @return string
     */
    protected function _getThemePath()
    {
        // loading current theme and current lang theme base directory
        $path = ZNF::THEME_DIR . "/{$_SESSION['znf']['theme']}/{$_SESSION['znf']['lang']}";

        if (!is_dir($path)) {
            // loading current theme and default lang theme base directory
            $path = ZNF::THEME_DIR . "/{$_SESSION['znf']['theme']}/{$this->_appConfig->getLang()}";

            if (!is_dir($path)) {
                // loading default theme and default lang theme base directory
                $path = ZNF::THEME_DIR . "/{$this->_appConfig->getTheme()}/{$this->_appConfig->getLang()}";

                if (!is_dir($path)) {
                    // default theme and default lang theme base dir not found
                    $translation = new ZNF_Presentation_Translation('ZNF', $_SESSION['znf']['lang']);
                    throw new ZNF_Presentation_RenderException($translation->get('errorLoadThemeBaseDir'));
                }

            }

        }

        return $path;
    }


    /**
     * Assigns values to template parameters.
     *
     * @access public
     * @param string $param The template parameter name
     * @param mixed $value The value to assign
     */
    abstract public function assign($param, $value);

    /**
     * Executes the renderization and displays the result.
     *
     * @access public
     * @param string $template File name of the template
     * @param mixed $data Additional data to process
     */
    abstract public function display($template, $data = null);

    /**
     * Executes the renderization and returns the result.
     *
     * @access public
     * @param string $template File name of the template
     * @param mixed $data Additional data to process
     * @return string
     */
    abstract public function fetch($template, $data = null);

}

/**
 * <i>ZNF_Presentation_RenderException</i> is the exception type for the <i>ZNF_Presentation_Render</i> class.
 *
 * <i>ZNF_Presentation_RenderException</i> extends the <i>Exception</i> class of PHP5.
 *
 * @access     public
 * @package    ZNF
 * @subpackage Presentation
 * @author     Alessandro Rossini <http://www.alessandrorossini.org>
 * @author     Graziano Liberati <http://www.liberati.org>
 * @copyright  2004-2007 The ZNF Development Team
 * @license    LGPL License 2.1 <http://www.gnu.org/copyleft/lesser.html>
 * @version    SVN $Id: Render.php 44 2007-07-22 23:55:30Z evolic $
 * @since      Release 0.5.0
 * @link       http://www.zeronotice.org
 */
class ZNF_Presentation_RenderException extends Exception
{
}

?>
